<?php
class Pageley_Gateway_Database_MySql_Connection implements Pageley_Gateway_Database_Connection {
    private $connection;
    private $valid;

    public function __construct($host, $port, $username, $password) {
    	try {
            $this->connection = mysql_connect($host . ":" . $port, $username, $password, true);
    	} catch(Exception $e) {
            throw new Pageley_Gateway_Database_ConnectionException(
                "Failed to connect to MySQL database at '" . $host . "' with user '" .
                $username . "'. Reason: " . $e->getMessage(), E_WARNING, $e);
        }
        $this->valid = true;
        $this->executeSql("SET SQL_MODE = 'ANSI_QUOTES'");
        $this->executeSql("SET NAMES utf8");
        $this->executeSql("SET autocommit = 0");
        $this->executeSql("BEGIN");
    }
    
    public function getId() {
    	return mysql_thread_id($this->connection);
    }

    public function commit() {
        $this->executeSql("COMMIT");
    }

    public function rollback() {
        $this->executeSql("ROLLBACK");
    }

    public function prepareStatement($sql) {
        return new Pageley_Gateway_Database_MySql_Statement($sql, $this);
    }
    
    public function getResource() {
    	return $this->connection;
    }

    private function executeSql($sql) {
        if($this->valid) {
        	try {
        		mysql_query($sql, $this->connection);
        	} catch(Exception $e) {
        		$errorNumber = $this->getErrorNumber();
        		if($errorNumber == 0) {
        			$this->valid = false;
        		}
        		$message = $this->getErrorMessage();
        		throw new Pageley_Gateway_Database_Exception("Failed to execute statement [" . $sql . "].\n Error "
                    . $errorNumber . ": " . $message, E_WARNING, $e);
        	}
        } else {
            throw new Pageley_Gateway_Database_Exception("Failed to execute statement [" . $sql . "].\n Error "
                . "-1: Connection closed.");
        }
    }
    
    private function getErrorMessage() {
        $message = "unable to determine";
        try {
            $message = mysql_error($this->connection);
        } catch(Exception $e) {
        	if(Pageley_Log::isDebugEnabled($this)) {
        	   Pageley_Log::debug($this, "Unable to determine error. Reason: " . $e->getMessage());
        	}
        }
        return $message;
    }
    
    private function getErrorNumber() {
        $errorNumber = 0;
        try {
            $errorNumber = mysql_errno($this->connection);
        } catch(Exception $e) {
        	if(Pageley_Log::isDebugEnabled($this)) {
        		Pageley_Log::debug($this, "Unable to determine error number. Reason: " . $e->getMessage());
        	}
        }
        return $errorNumber;
    }

    public function close() {
    	try {
    	    $this->valid = false;
    		mysql_close($this->connection);
    	} catch(Exception $e) {
    		if(Pageley_Log::isDebugEnabled($this)) {
    		    Pageley_Log::debug($this, "Unable to determine close connection. Reason: " . $e);
    		}
    	}
    	
    }
}